const koa = require('koa');
const app = new koa();
const Router = require("koa-router");
const router = new Router();
const cors = require("koa2-cors");
const koaBody = require("koa-body");

const ENV = "test-nin9g";

// 跨域
app.use(cors({
  origin: ["http://localhost:9528"],
  credentials: true
}));

// 接收post参数解析
app.use(koaBody({
  multipart: true,
}));

app.use(async (ctx, next) => {
  console.log("全局中间键");
  // ctx.body = "Hello Wolrd";
  ctx.state.env = ENV;
  await next();
});

const playlist = require("./controller/playlist.js"); // 导入当前获取歌单文件
const swiper = require("./controller/swiper.js"); 
const blog = require("./controller/blog.js"); 

router.use("/playlist", playlist.routes()); // 定义一个路由方法
router.use("/swiper", swiper.routes()); 
router.use("/blog", blog.routes()); 

app.use(router.routes()); // 对 router 下面的 routes 方法进行声明
app.use(router.allowedMethods()); // 允许方法的使用

app.listen(3000, () => {
  console.log("服务开启在3000端口")
});

